﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace ShopB2C.Core.Helpers
{
    public class EnumHelper
    {
        /// <summary>
        /// 获取指定枚举中，对应枚举的 Description 的 Attribute 的值，
        /// 与枚举的索引组成匿名对象加入到 List 中返回，new { Text = text, Value = value }
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <returns>List</returns>
        public static List<object> GetEnumDescriptionToList(Type enumType)
        {
            if (enumType.IsEnum)
            {
                int value;
                string text;
                object[] attr;
                List<object> resultList = new List<object>();
                Type descriptionType = typeof(DescriptionAttribute);

                foreach (FieldInfo fi in enumType.GetFields())
                {
                    if (fi.FieldType.IsEnum)
                    {
                        attr = fi.GetCustomAttributes(descriptionType, true);
                        if (attr.Length == 0)
                        {
                            throw new FormatException("enum 类型错误");
                        }
                        text = attr.Length > 0 ? ((DescriptionAttribute)attr[0]).Description : fi.Name;
                        value = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                        resultList.Add(new { Text = text, Value = value });
                    }
                }
                return resultList;
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
        /// <summary>
        /// 获取指定枚举,
        /// 与枚举的索引组成匿名对象加入到 List 中返回，new { Text = text, Value = value }
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <returns>List</returns>
        public static List<object> GetEnumToList(Type enumType)
        {
            if (enumType.IsEnum)
            {
                int value;
                string text;
                List<object> resultList = new List<object>();

                foreach (FieldInfo fi in enumType.GetFields())
                {
                    if (fi.FieldType.IsEnum)
                    {
                        text = fi.Name;
                        value = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                        resultList.Add(new { Text = text, Value = value });
                    }
                }
                return resultList;
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
        /// <summary>
        /// 根据 value 读取指定枚举中的 Description 的 Attribute 的值，
        /// </summary>
        /// <param name="enumType">指定枚举的 Type</param>
        /// <param name="value">值</param>
        /// <returns>string</returns>
        public static string GetEnumsDescription(Type enumType, int value)
        {
            if (enumType.IsEnum)
            {
                int val;
                string text;
                object[] attr;
                Type descriptionType = typeof(DescriptionAttribute);

                foreach (FieldInfo fi in enumType.GetFields())
                {
                    if (fi.FieldType.IsEnum)
                    {
                        attr = fi.GetCustomAttributes(descriptionType, true);
                        if (attr.Length == 0)
                        {
                            throw new FormatException("enum 类型错误");
                        }
                        text = attr.Length > 0 ? ((DescriptionAttribute)attr[0]).Description : fi.Name;
                        val = (int)enumType.InvokeMember(fi.Name, BindingFlags.GetField, null, null, null);
                        if (value == val)
                        {
                            return text;
                        }
                    }
                }
                return string.Empty;
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
        /// <summary>
        /// 通过枚举的名称返回枚举
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="name">枚举的名称</param>
        /// <returns>T</returns>
        protected T GetEnumByName<T>(string name)
        {
            Type orderStatusType = typeof(T);
            if (orderStatusType.IsEnum)
            {
                var result = Enum.Parse(orderStatusType, name, true);
                if (Enum.IsDefined(orderStatusType, result))
                {
                    return (T)result;
                }
                else
                {
                    throw new IndexOutOfRangeException("未找到指定的信息");
                }
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
        /// <summary>
        /// 通过枚举返回单据状态字符串
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="enumType">枚举</param>
        /// <returns>string</returns>
        protected string GetNameByEnum<T>(T enumType)
        {
            if (typeof(T).IsEnum)
            {
                return Enum.GetName(typeof(T), enumType);
            }
            else
            {
                throw new ArgumentException("必须是 enum 类型");
            }
        }
    }
}
